/*
 * Copyright (c) 2011 Norbert Kopcsek
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.modulr.bundle {

	import flash.utils.Dictionary;

	import mx.collections.ArrayList;
	import mx.collections.IList;

	import org.modulr.feature.Feature;
	import org.modulr.framework.Framework;
	import org.modulr.service.IServiceFilter;
	import org.modulr.service.IServiceListener;
	import org.modulr.service.IServiceReference;
	import org.modulr.service.IServiceRegistration;
	import org.modulr.service.ServiceEvent;

	/**
	 * A bundle's execution context within the Framework. The context is used to grant access to other methods so that this bundle can interact with the Framework.
	 *
	 * @see org.modulr.bundle.Bundle
	 * @see org.modulr.framework.Framework
	 *
	 * @author Norbert Kopcsek
	 */
	public class BundleContext implements IBundleContext {


		//------------------------------------------------------------------------------
		//
		//   Constructor 
		//
		//------------------------------------------------------------------------------

		public function BundleContext( framework:Framework, bundle:Bundle ) {
			_framework = framework;
			_bundle = bundle;
			_properties = new Dictionary()
		}


		//------------------------------------------------------------------------------
		//
		//   Properties (Getter/Setter) 
		//
		//------------------------------------------------------------------------------

		//--------------------------------------
		// properties 
		//--------------------------------------

		public function get properties():Dictionary {
			return _properties;
		}

		//--------------------------------------
		// bundle 
		//--------------------------------------

		/**
		 * The bundle object for this bundle context.
		 */
		public function get bundle():Bundle {
			return _bundle;
		}

		//--------------------------------------
		// framework 
		//--------------------------------------

		/**
		 * Reference to the framework this bundle context belongs to.
		 */
		public function get framework():Framework {
			return _framework;
		}

		//--------------------------------------
		// servicesInUse 
		//--------------------------------------

		public function get servicesInUse():IList {
			return _servicesInUse;
		}

		public function set servicesInUse( value:IList ):void {
			_servicesInUse = value;
		}


		//------------------------------------------------------------------------------
		//
		//   Protected fields 
		//
		//------------------------------------------------------------------------------

		/**
		 * @private
		 */
		protected var _properties:Dictionary;

		/**
		 * @private
		 */
		protected var _bundle:Bundle;

		/**
		 * @private
		 */
		private var _framework:Framework;

		/**
		 * @private
		 */
		private var _servicesInUse:IList;


		//------------------------------------------------------------------------------
		//
		//  Methods 
		//
		//------------------------------------------------------------------------------

		/**
		 * Adds the specified IBundleListener object to the context bundle's list of listeners.
		 */
		public function addBundleListener( listener:IBundleListener ):void {
			framework.addBundleListener( listener );
		}

		/**
		 * Removes the specified IBundleListener object from the context bundle's list of listeners.
		 */
		public function removeBundleListener( listener:IBundleListener ):void {
			framework.removeBundleListener( listener );
		}

		/**
		 * Adds the specified IServiceListener object to the context bundle's list of listeners.
		 */
		public function addServiceListener( listener:IServiceListener ):void {
			framework.addServiceListener( listener );
		}

		/**
		 * Removes the specified IServiceListener object from the context bundle's list of listeners.
		 */
		public function removeServiceListener( listener:IServiceListener ):void {
			framework.removeServiceListener( listener );
		}

		/**
		 * Returns a bundle for the given id.
		 * @param id The id of the bundle.
		 * @return The bundle with the given id or null.
		 */
		public function getBundleById( id:String ):Bundle {
			return framework.getBundleById( id );
		}

		/**
		 * Activates the given bundle
         * @param bundle The bundle to be activated.
		 */
        // TODO The developer should no be able to call this.
        // At least it should check whether the bundle can be activated or not..
		public function activateBundle( bundle:Bundle ):void {
			if ( bundle == null ) { // this or error
				return;
			}
			framework.activateBundle( bundle );
		}

		public function registerService( clazz:String, service:Object, properties:Object = null ):IServiceRegistration {
			return framework.serviceRegistry.registerService( this, clazz, service, properties );
		}

		/**
		 * FITS
		 */
		public function getServiceReference( clazz:String ):IServiceReference {
			var registrations:IList = framework.serviceRegistry.lookupByClazz( clazz ) as IList;

			if ( registrations != null && registrations.length > 0 ) {
				return IServiceRegistration( registrations.getItemAt( 0 )).reference;
			}
			return null;
		}

		/**
		 * FITS
		 */
		public function getServiceReferences( clazz:String ):IList {
			var registrations:IList = framework.serviceRegistry.lookupByClazz( clazz ) as IList;
			var references:IList = new ArrayList();

			for ( var i:int = 0; i < registrations.length; i++ ) {
				var registration:IServiceRegistration = registrations.getItemAt( i ) as IServiceRegistration;
				references.addItem( registration.reference );
			}
			return references;
		}

		/**
		 * FITS
		 */
		public function getService( reference:IServiceReference ):Object {
			return reference.registration.getService( this );
		}

		/**
		 * FITS
		 */
		public function ungetService( reference:IServiceReference ):Boolean {
			return reference.registration.ungetService( this );
		}
	}
}

